<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

require_once(dirname(__FILE__) . "/../common/Logger.php");
require_once(dirname(__FILE__) . "/../common/Common.php");
require_once(dirname(__FILE__) . "/../dbcom/OrderCom.php");
require_once(dirname(__FILE__) . "/../dbcom/ReservetimeCom.php");
require_once(dirname(__FILE__) . "/../dbcom/RagionCom.php");
require_once(dirname(__FILE__) . "/../dbcom/UsertypeCom.php");
require_once(dirname(__FILE__) . "/../dbcom/ServiceCom.php");
require_once(dirname(__FILE__) . "/../dbcom/StaisCom.php");
require_once(dirname(__FILE__) . "/../dbcom/AppointCom.php");

$log = new Logger("./../saveorderapi.log");
$common = new Common();
$ordercom = new OrderCom();
$reservetimecom = new ReservetimeCom();
$ragioncom = new RagionCom();
$usertypecom = new UsertypeCom();
$servicecom = new ServiceCom();
$staiscom = new StaisCom();
$appointcom = new AppointCom();

if (!isset($_REQUEST['cont']) || empty($_REQUEST['cont'])) {
    echo json_encode(array("code" => 100, "msg" => "cannot find cont"));
    exit();
}

switch ($_REQUEST['cont']) {
    case 1://初次进入
        $result = array("code" => 0, "msg" => "success", "data" => array());
        $ragionarr = $ragioncom->getRaginByType("1");
        if (!$ragionarr) {
            echo json_encode(array("code" => 400, "msg" => "cannot find ragion"));
            exit();
        }
        foreach ($ragionarr as $k1 => $v1) {
            $result["data"]["ragion"][$k1]['id'] = $v1['id'];
            $result["data"]["ragion"][$k1]['name'] = $v1['name'];
        }
        $usertypearr = $usertypecom->getUserTypearr();
        if (!$usertypearr) {
            echo json_encode(array("code" => 400, "msg" => "cannot find usertypelist"));
            exit();
        }
        foreach ($usertypearr as $k2 => $v2) {
            $result["data"]["usertype"][$k2]['id'] = $v2['id'];
            $result["data"]["usertype"][$k2]['name'] = $v2['name'];
        }
        $servicearr = $servicecom->getServicearr();
        if (!$servicearr) {
            echo json_encode(array("code" => 400, "msg" => "cannot find servicelist"));
            exit();
        }
        foreach ($servicearr as $k3 => $v3) {
            $result["data"]["service"][$k3]['id'] = $v3['id'];
            $result["data"]["service"][$k3]['name'] = $v3['name'];
            $result["data"]["service"][$k3]['fee'] = sprintf("%.2f", $v3['fee'] / 100);
            $result["data"]["service"][$k3]['number'] = 0;
            $result["data"]["service"][$k3]['isncard'] = 0;
        }
        echo json_encode($result);
        break;
    case 2://点击选择区域后
        if (!isset($_REQUEST['type']) || empty($_REQUEST['type'])) {
            echo json_encode(array("code" => 100, "msg" => "cannot find type"));
            exit();
        }
        $type = $_REQUEST['type'] - 1;
        if ($type == 0) {
            $streetarr = $ragioncom->getRaginByType($type);
        } else {
            if (!isset($_REQUEST['parentid']) || empty($_REQUEST['parentid'])) {
                echo json_encode(array("code" => 100, "msg" => "cannot find parentid"));
                exit();
            }
            $streetarr = $ragioncom->getRaginByType($type, $_REQUEST['parentid']);
        }

        if (!$streetarr) {
            echo json_encode(array("code" => 400, "msg" => "cannot find streetlist"));
            exit();
        }
        $result = array("code" => 0, "msg" => "success", "data" => array());
        foreach ($streetarr as $k => $v) {
            $result["data"][$k]['id'] = $v['id'];
            $result["data"][$k]['name'] = $v['name'];
            $result["data"][$k]['selected'] = false;
            $result["data"][$k]['parent_id'] = $v['parent_id'];
            $result["data"][$k]['type'] = $v['type'] + 1;
        }
        echo json_encode($result);
        break;
    case 3://选择街道后
        if (!isset($_REQUEST['streetid']) || empty($_REQUEST['streetid'])) {
            echo json_encode(array("code" => 100, "msg" => "cannot find streetid"));
            exit();
        }
        $streetarr = $ragioncom->getRaginByType($_REQUEST['streetid']);
        if (!$streetarr) {
            echo json_encode(array("code" => 400, "msg" => "cannot find communitylist"));
            exit();
        }
        $result = array("code" => 0, "msg" => "success", "data" => array());
        foreach ($streetarr as $k => $v) {
            $result["data"][$k]['id'] = $v['id'];
            $result["data"][$k]['name'] = $v['name'];
            $result["data"][$k]['selected'] = false;
            $result["data"][$k]['parent_id'] = $v['parent_id'];
            $result["data"][$k]['type'] = 2;
        }
        echo json_encode($result);
        break;
    case 4://选择社区后
        if ((!isset($_REQUEST['time']) || empty($_REQUEST['time'])) || (!isset($_REQUEST['communityid']) || empty($_REQUEST['communityid']))) {
            echo json_encode(array("code" => 100, "msg" => "cannot find time or communityid"));
            exit();
        }
        $rtimelist = $reservetimecom->getRtimelist();
        if (!$rtimelist) {
            echo json_encode(array("code" => 400, "msg" => "cannot find rtimelist"));
            exit();
        }
        $timetr = $_REQUEST['time'];
        $time = strtotime($timetr);
        $key = 0;
        $result = array("code" => 0, "msg" => "success", "data" => array());
        foreach ($rtimelist as $k => $v) {
            //$res = $ordercom->checkOrderTime($time, $_REQUEST['communityid'], $v['id']);
            $res = $appointcom->checkOrderTime($time,$_REQUEST['communityid'], $v['id']);
            if (empty($res)) {
                $result["data"][$key]['id'] = $v['id'];
                $result["data"][$key]['info'] = $v['info'];
                $key++;
            }
        }
        echo json_encode($result);
        break;
    case 5://拉去order历史
        if (!isset($_REQUEST['userid']) || empty($_REQUEST['userid'])) {
            echo json_encode(array("code" => 100, "msg" => "cannot find userid"));
            exit();
        }
        $userid = $_REQUEST['userid'];
        $orderhistory = $ordercom->getOrderHistory($userid);
        if (!$orderhistory) {
            echo json_encode(array("code" => 400, "msg" => "get history fail"));
            exit();
        }
        $result = array("code" => 0, "msg" => "success", "data" => array());
        if (!empty($orderhistory)) {
            
        }
        echo json_encode($result);
        break;
    case 6://拉取appoint历史
        if (!isset($_REQUEST['userid']) || empty($_REQUEST['userid'])) {
            echo json_encode(array("code" => 100, "msg" => "cannot find userid"));
            exit();
        }
        $userid = $_REQUEST['userid'];
        $appointhistory = $appointcom->getAppointHistory($userid);
        if (!$appointhistory) {
            echo json_encode(array("code" => 400, "msg" => "get history fail"));
            exit();
        }
        $result = array("code" => 0, "msg" => "success", "data" => array());
        if(!empty($appointhistory)){
            $result['data']['address'] = $appointhistory['address'];
            $result['data']['usertype'] = $appointhistory['usertype_id'];
            $result['data']['floor'] = $appointhistory['floor'];
            $result['data']['area'] = $appointhistory['area_id'];
            $result['data']['street'] = $appointhistory['street_id'];
            $result['data']['ommunity'] = $appointhistory['community_id'];
            $result['data']['codeid'] = $appointhistory['codeid'];
            $result['data']['sex'] = $appointhistory['sex'];
            $result['data']['name'] = $appointhistory['username'];
            $result['data']['mobile'] = $appointhistory['mobile'];
        }
        echo json_encode($result);
        break;
    default :
        echo json_encode(array("code" => 500, "msg" => "undefined error"));
        break;
}

exit();
